/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Core.Customer;
import Core.Customers;
import Core.Util;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.xml.sax.SAXException;

/**
 *
 * @author vietean
 */
public class Register extends HttpServlet {
    boolean isRegister = false;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SAXException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Register Page</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Register a account:</h1>");
            if( isRegister )
            {
                out.println("<b style='color:red;'>Thanks for your register!</b>");
            }
            out.println("<form method='POST'>");
            out.println("<dl>");
                out.println("<dt>Username</dt>");
                out.println("<dd><input name='Username' /></dd>");

                out.println("<dt>Password</dt>");
                out.println("<dd><input name='Password' /></dd>");

                out.println("<dt>Fullname</dt>");
                out.println("<dd><input name='Fullname' /></dd>");

                out.println("<dt>Email</dt>");
                out.println("<dd><input name='Email' /></dd>");

                out.println("<dt>Address</dt>");
                out.println("<dd><input name='Address' /></dd>");

                out.println("<dt>PhoneNo</dt>");
                out.println("<dd><input name='PhoneNo' /></dd>");

                out.println("<dt>Birthday</dt>");
                out.println("<dd><input name='Birthday' /></dd>");
                
                out.println("<dt>Submit</dt>");
                out.println("<dd><input type='Submit' value='Register' /></dd>");
            out.println("</dl>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SAXException ex) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            /* TODO output your page here. You may use following sample code. */
            Customers customerTbl = Util.getCustomerTbl(getServletContext().getRealPath("/Data/Customers.xml"));
            String Username = request.getParameter("Username");
            String Password = request.getParameter("Password");
            String Email = request.getParameter("Email");
            String Address = request.getParameter("Address");
            String Fullname = request.getParameter("Fullname");
            String PhoneNo = request.getParameter("PhoneNo");
            String Birthday = request.getParameter("Birthday");
            Customer c = new Customer(Fullname, Username, Password, Birthday, PhoneNo, Address, Email);
            if( customerTbl.insert(c) )
            {
                isRegister = true;
            }
            processRequest(request, response);
        } catch (SAXException ex) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
